package com.kgc.rb.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.kgc.rb.common.kafka.KafkaTools;
import com.kgc.rb.common.tools.Constants;
import com.kgc.rb.common.tools.TokenTools;
import com.kgc.rb.pojo.QgUser;
import com.kgc.rb.service.QgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author Administrator
 */
@RestController
public class TestController {

    @Reference
    private QgUserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private KafkaTools kafkaTools;

    @RequestMapping(value = "/userindex")
    public List<QgUser> querAll() {
        Map<String, Object> params = new HashMap<>();
        try {
            kafkaTools.sendInfoMessage("*******************所有用户的信息查询成功******************");
            return userService.getQgUserListByMap(params);
        } catch (Exception e) {
            e.printStackTrace();
            kafkaTools.sendErrorMessage(e);
        }
        return null;
    }

    @RequestMapping(value = "/testuserlogin")
    public String testuserLogin() {

        for (int i = 0; i < 1100; i++) {
            QgUser qgUser = new QgUser();
            qgUser.setId(i + "");
            String phone = "15885458741";
            if (i < 10) {
                phone = "1588545874" + i;
            } else if (i >= 10 && i < 100) {
                phone = "158854587" + i;
            } else {
                phone = "15885458" + i;
            }
            qgUser.setPhone(phone);
            qgUser.setRealName("李青" + i);
            qgUser.setCreatedTime(new Date());
            //将token:userid值作为key，token作为置存入redis中
            String token = Constants.TOKEN_PREFIX + TokenTools.createToken(qgUser.getId(), qgUser.getPhone());
            //redisTemplate.opsForValue().set(Constants.TOKEN_PREFIX + qgUser.getId(), token);
            //2.将token作为key用户的信息作为值保存到Redis中
            redisTemplate.opsForValue().set(Constants.TOKEN_PREFIX + qgUser.getId(), qgUser);
        }
        return "添加成功";
    }
}